package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Transactional
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {


    @Autowired
    private CheckGroupDao checkGroupDao;


    /**
     * 分页条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Result queryPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        List<CheckGroup> list = checkGroupDao.findByCondition(queryPageBean.getQueryString());

        Result result;

        if (list.isEmpty()) {
            result = new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        } else {
            result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, new PageInfo<>(list));
        }

        return result;
    }


    /**
     * 通过id查询检查组
     *
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {

        CheckGroup checkGroup = checkGroupDao.findById(id);

        if (checkGroup == null)
            throw new RuntimeException(MessageConstant.QUERY_CHECKGROUP_FAIL);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);

    }


    /**
     * 通过检查组id 查询包含的检查项id
     *
     * @param checkGroupId
     * @return
     */
    @Override
    public Result findCheckItemIdsByCheckGroup(Integer checkGroupId) {
        List<Integer> list = checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);
        if (list.isEmpty())
            throw new RuntimeException(MessageConstant.QUERY_CHECKITEM_FAIL);


        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
    }


    /**
     * 删除检查组
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteCheckGroup(Integer id) {

        /*
            删除检查组的时候需要先将中间表中的关联关系删除掉
            然后再删除中间表
         */
        Integer count;
        //1.先删除中间表的关联关系
        count = checkGroupDao.deleteAssocication(id);
        if (count == 0)
            throw new RuntimeException(MessageConstant.DELETE_CHECKGROUP_FAIL);

        count = checkGroupDao.deleteCheckGroup(id);

        if (count == 0)
            throw new RuntimeException(MessageConstant.DELETE_CHECKGROUP_FAIL);

        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 新增检查组
     *
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @Override
    public Result addCheckGroup(CheckGroup checkGroup, Integer[] checkItemIds) {

        //向检查组中添加数据时 获取插入的id 跟检查项的id一起插入到中间表中
        Integer count;
        count = checkGroupDao.saveCheckGroup(checkGroup);
        if (count == 0)
            throw new RuntimeException(MessageConstant.ADD_CHECKGROUP_FAIL);

        for (Integer checkItemId : checkItemIds) {


            //取到每一个检查项ID循环添加到中间表中
            count = checkGroupDao.setCheckGroupAndCheckItem(checkGroup.getId(), checkItemId);

            if (count == 0)
                throw new RuntimeException(MessageConstant.ADD_CHECKGROUP_FAIL);
        }

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 编辑检查组
     *
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @Override
    public Result editCheckGroup(CheckGroup checkGroup, Integer[] checkItemIds) {

        /**
         * 编辑检查组实际就是 在更新完检查组之后 删除中间表原有的关联关系 再添加新的关联关系
         *
         */
        Integer count;
        count = checkGroupDao.update(checkGroup);

        if (count == 0)
            throw new RuntimeException(MessageConstant.EDIT_CHECKGROUP_FAIL);

        //删除中间表中的关系
        checkGroupDao.deleteAssocication(checkGroup.getId());

        //添加新的关系
        for (Integer checkItemId : checkItemIds) {

            count = checkGroupDao.setCheckGroupAndCheckItem(checkGroup.getId(), checkItemId);
            if (count == 0)
                throw new RuntimeException(MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public Result findAll() {

        List<CheckGroup> list = checkGroupDao.findAll();

        if (list.isEmpty())
            throw new RuntimeException(MessageConstant.QUERY_CHECKGROUP_FAIL);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);

    }

    /**
     * 通过套餐id查询检查组id
     * @param setmealId
     * @return
     */
    @Override
    public Result findCheckGroupIdsBySetmealId(Integer setmealId) {
        List<Integer> list = checkGroupDao.findCheckGroupIdsBySetmealId(setmealId);
        if (list.isEmpty())
            throw new RuntimeException(MessageConstant.QUERY_CHECKGROUP_FAIL);


        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }


}
